package com.zwxbc.funplay.service.impl;

import com.zwxbc.funplay.dao.PlayerGameLibraryDao;
import com.zwxbc.funplay.dao.impl.PlayerGameLibraryDaoImpl;
import com.zwxbc.funplay.entity.Game;
import com.zwxbc.funplay.entity.PlayerGameLibrary;
import com.zwxbc.funplay.service.GameService;
import com.zwxbc.funplay.service.PlayerGameLibraryService;

import java.util.ArrayList;
import java.util.List;

public class PlayerGameLibraryServiceImpl implements PlayerGameLibraryService {
	PlayerGameLibraryDao playerGameLibraryDao = new PlayerGameLibraryDaoImpl();

	@Override
	public List<PlayerGameLibrary> getPlayerGameLibraryList() {
		return playerGameLibraryDao.getAllPlayerGameLibraries();
	}

	@Override
	public boolean deletePlayerGameLibrary(Long playerGameLibraryId) {
		int result = playerGameLibraryDao.deletePlayerGameLibrary(playerGameLibraryId);
		return result > 0;
	}

	@Override
	public boolean updatePlayerGameLibrary(PlayerGameLibrary playerGameLibrary) {
		int result = playerGameLibraryDao.updatePlayerGameLibrary(playerGameLibrary);
		return result > 0;
	}

	@Override
	public boolean addPlayerGameLibrary(PlayerGameLibrary playerGameLibrary) {
		int result = playerGameLibraryDao.addPlayerGameLibrary(playerGameLibrary);
		return result > 0;
	}

	@Override
	public boolean playerIfHaveGame(Long gameId, Long playerId) {
		int result = playerGameLibraryDao.playerIfHaveGame(gameId, playerId);
		return result > 0;
	}

	@Override
	public PlayerGameLibrary getPlayerGameLibraryById(Long playerGameLibraryId) {
		return playerGameLibraryDao.getPlayerGameLibraryById(playerGameLibraryId);
	}

	@Override
	public List<Game> getGameListByPlayerId(Long playerId) {
		List<PlayerGameLibrary> gameListByPlayer = playerGameLibraryDao.getGameListByPlayerId(playerId);
		List<Game> gameList = new ArrayList<Game>();
		GameService gameService = new GameServiceImpl();
		for (PlayerGameLibrary playerGameLibrary : gameListByPlayer) {
			Game game = gameService.getGameById(playerGameLibrary.getGameId());
			if (game != null) {
				gameList.add(game);
			}
		}
		return gameList;
	}
}
